package com.a214.yxz.yuejian013.Fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.a214.yxz.yuejian013.Activity.ClubDetailActivity;
import com.a214.yxz.yuejian013.Activity.ClubSearchActivity;
import com.a214.yxz.yuejian013.Base.BaseSFragment;
import com.a214.yxz.yuejian013.JavaBean.ClubBean;
import com.a214.yxz.yuejian013.Adapter.ClubListAdapter;
import com.a214.yxz.yuejian013.Listener.ServiceListener;
import com.a214.yxz.yuejian013.Model.ResModel;
import com.a214.yxz.yuejian013.R;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class ClubFragment extends BaseSFragment {
    private ImageButton searchBtn;
    private RadioGroup radioGroup;
    private RecyclerView recyclerView;
    private ClubListAdapter adapter;
    private RecyclerView.LayoutManager layoutManager;
    private ServiceListener<List<ClubBean>> listener;
    private ResModel model = new ResModel("gson",getSessionID());


    public ClubFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_club, container, false);

        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        searchBtn = (ImageButton) view.findViewById(R.id.club_fragment_search);
        radioGroup = (RadioGroup) view.findViewById(R.id.clubList_RadioGroup);
        recyclerView = (RecyclerView) view.findViewById(R.id.club_list);

        adapter = new ClubListAdapter(getContext());
        layoutManager = new LinearLayoutManager(getContext());
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(layoutManager);

        listener = new ServiceListener<List<ClubBean>>() {
            @Override
            public void onResponse(final List<ClubBean> l) {
                adapter.setList(l);
                adapter.setOnItemClickListener(new ClubListAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        Intent intent = new Intent(getContext(), ClubDetailActivity.class);
                        intent.putExtra("club_id", l.get(position).getId());
                        intent.putExtra("pic",l.get(position).getThumb());
                        getContext().startActivity(intent);
                    }

                    @Override
                    public void onItemLongClick(View view, int position) {

                    }
                });
            }

            @Override
            public void onFail(String msg) {

            }
        };

        searchBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(), ClubSearchActivity.class);
                getContext().startActivity(intent);
            }
        });

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i) {
                    case R.id.clubList_R_p:
                        model.getList("club",getUserId(), 1,"hits", listener);
                        break;
                    case R.id.clubList_R_d:
                        model.getList("club",getUserId(), 1,"listorder", listener);
                        break;
                }
            }
        });
        RadioButton btn= (RadioButton) view.findViewById(R.id.clubList_R_p);
        btn.setChecked(true);
    }
}
